﻿using Microsoft.AspNetCore.Mvc;
using SMARTHOTEL.hotel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SMARTHOTEL.RoomMoneys
{
    /// <summary>
    /// 房价服务
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class RoomMoneyService : ApplicationService, IRoomMoneyService
    {
        IRepository<RoomMoney> romeyRep;

        IRepository<RoomType> romTypRep;

        public RoomMoneyService(IRepository<RoomMoney> romeyRep, IRepository<RoomType> romTypRep)
        {
            this.romeyRep = romeyRep;
            this.romTypRep = romTypRep;
        }


        /// <summary>
        /// 添加房价
        /// </summary>
        /// <param name="roomMoneyDto"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> CreateAllAsync(CreateUpdateRoomMoneyDto roomMoneyDto)
        {
            try
            {
                var roomMoney = ObjectMapper.Map<CreateUpdateRoomMoneyDto, RoomMoney>(roomMoneyDto);

                var roomtype = await romTypRep.GetAsync(x => x.RoomTypeName == roomMoneyDto.RoomTypeName);

                roomMoney.RoomTypeId = roomtype.Id;

                await romeyRep.InsertAsync(roomMoney);

                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "添加成功" };
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "添加失败" + ex.Message };
            }
        }

        /// <summary>
        /// 房价显示
        /// </summary>
        /// <param name="roomTypeId"></param>
        /// <returns></returns>0
        /// ol
        public Task<ResultDto<List<RoomMoneyDto>>> GetAllAsync(Guid? roomTypeId)
        {
            var list = from a in romeyRep.GetQueryableAsync().Result
                       join b in romTypRep.GetQueryableAsync().Result on a.RoomTypeId equals b.Id
                       where a.IsDeleted == false
                       select new RoomMoneyDto
                       {
                           Id = a.Id,
                           RoomTypeId = b.Id,
                           RoomTypeCash = b.RoomTypeCash,
                           RoomTypeName = b.RoomTypeName,
                           ProductName = a.ProductName,
                           RoomTypePersonNum= b.RoomTypePersonNum,
                           NormalPrice = a.NormalPrice,
                           PaymentTerm = a.PaymentTerm,
                           Accommodiscount = a.Accommodiscount,
                           BreakfirstNum = a.BreakfirstNum,
                           RoomNum = a.RoomNum,
                           SalesStrategy = a.SalesStrategy,
                           RoomState = a.RoomState,
                           IsDeleted = a.IsDeleted
                       };
            if (roomTypeId != null)
            {
                list = list.Where(x => x.RoomTypeId == roomTypeId);
            }

            return Task.FromResult(new ResultDto<List<RoomMoneyDto>> { Data = list.ToList(), Code = StatusCode.Success, Msg = "查询成功" });
        }
        /// <summary>
        /// 修改房价
        /// </summary>
        /// <param name="roomMoneyDto"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public async Task<ResultDto<bool>> UpdateAsync(CreateUpdateRoomMoneyDtos roomMoneyDto, Guid Id)
        {
            try
            {

                var roomtype = await romTypRep.GetAsync(x => x.RoomTypeName == roomMoneyDto.RoomTypeName);

                var roomMoney = await romeyRep.GetAsync(x => x.Id == Id);

                var entity = ObjectMapper.Map<CreateUpdateRoomMoneyDtos, RoomMoney>(roomMoneyDto, roomMoney);


                entity.RoomTypeId = roomtype.Id;

                await romeyRep.UpdateAsync(entity);

                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "修改成功" };

            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "修改失败" + ex.Message };
            }
        }
        /// <summary>
        /// 删除房价
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteAsync")]
        public async Task<ResultDto<bool>> DeleteAsync(Guid id)
        {
            try
            {
                var roomMoney = await romeyRep.GetAsync(x => x.Id == id);
                if (roomMoney == null)
                {
                    return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "删除失败，该数据不存在！" };
                }
                else
                {
                    roomMoney.IsDeleted = true;
                    await romeyRep.UpdateAsync(roomMoney);
                    return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "删除成功" };
                }
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "删除失败" + ex.Message };
            }
        }

        /// <summary>
        /// 修改状态（上架下架）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpPut("UpdateStateAsync")]
        public async Task<ResultDto<bool>> UpdateStateAsync(Guid id)
        {
            try
            {
                var roomMoney = await romeyRep.FindAsync(x => x.Id ==id);
                if (roomMoney == null)
                {
                    return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "修改失败，该数据不存在！" };
                }
                else
                {
                    roomMoney.RoomState = !roomMoney.RoomState;
                    await romeyRep.UpdateAsync(roomMoney);
                    return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "修改成功" };
                }
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Msg = "修改失败" + ex.Message };
            }
        }
    }
}
